package com.aaa.power.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.OperLog;
import com.aaa.power.mapper.OperLogMapper;
import com.aaa.power.service.OperLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 操作日志记录(OperLog)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-12 13:59:54
 */
@Service("operLogService")
public class OperLogServiceImpl implements OperLogService {
    @Resource
    private OperLogMapper operLogMapper;

    /**
     * 查询指定行数据
     *
     * @param pageNo   查询起始位置
     * @param pageSize 查询条数
     * @param operLog
     * @return Result ---- Result.getData() 对象列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, OperLog operLog) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<OperLog> pageInfo = new PageInfo<>(operLogMapper.queryAll(operLog));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }


    /**
     * 通过ID查询单条数据
     *
     * @param operId 主键
     * @return 实例对象
     */
    @Override
    public OperLog queryById(Integer operId) {
        return this.operLogMapper.queryById(operId);
    }

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<OperLog> queryAllByLimit(int offset, int limit) {
        return this.operLogMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param operLog 实例对象
     * @return 对象列表
     */
    @Override
    public List<OperLog> queryAll(OperLog operLog) {
        return this.operLogMapper.queryAll(operLog);
    }

    /**
     * 新增数据
     *
     * @param operLog 实例对象
     * @return 实例对象
     */
    @Override
    public OperLog add(OperLog operLog) {
        this.operLogMapper.insert(operLog);
        return operLog;
    }

    /**
     * 修改数据
     *
     * @param operLog 实例对象
     * @return 实例对象
     */
    @Override
    public OperLog modify(OperLog operLog) {
        this.operLogMapper.update(operLog);
        return this.queryById(operLog.getOperId());
    }

    /**
     * 通过主键删除数据
     *
     * @param operId 主键
     * @return 是否成功
     */
    @Override
    public boolean delById(Integer operId) {
        return this.operLogMapper.deleteById(operId) > 0;
    }
}